package org.whuims.pois.action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.dao.NewsDao;
import org.whuims.pois.helper.ReportDataHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.List;
import java.util.Map;


public class ReportsAction extends DispatchAction {
    private NewsDao newsDao = new NewsDao();

    /**
     * 查看时间序列图
     */
    public ActionForward timeserial(ActionMapping mapping, ActionForm form,
                                    HttpServletRequest request, HttpServletResponse response) {
        String startDate = request.getParameter("sd");
        String endDate = request.getParameter("ed");
        String type = request.getParameter("type");
        long topic = getTopic(request);
        if (topic <= 0) {
            return mapping.findForward("login");
        }
        List<List<Calendar>> list = newsDao.timeserial(type, topic, null, startDate, endDate);
        List<Map<String, Long>> result = ReportDataHelper.timedata(list); //list包含三个系列（未实现），map是不同时间的数量分布

        String data = ReportDataHelper.jqPlotData(result, false);
        System.out.println("timeserial data:" + data);
        request.setAttribute("topicId", topic);
        request.setAttribute("startDate", startDate);
        request.setAttribute("endDate", endDate);
        request.setAttribute("data", data);  //plot data
        request.setAttribute("type",type);
        return mapping.findForward("timeserial");
    }

    /**
     * 查看站点分布图
     */
    public ActionForward sitedist(ActionMapping mapping, ActionForm form,
                                  HttpServletRequest request, HttpServletResponse response) {
        String startDate = request.getParameter("sd");
        String endDate = request.getParameter("ed");
        String type = request.getParameter("type");
        long topic = getTopic(request);
        List<Map<String, Long>> result = newsDao.sitedist(type, topic, null, startDate, endDate);

        String data = ReportDataHelper.jqPlotData(result, true);
        System.out.println("sitedist data:" + data);
        request.setAttribute("topicId", topic);
        request.setAttribute("startDate", startDate);
        request.setAttribute("endDate", endDate);
        request.setAttribute("data", data);
        request.setAttribute("type",type);
        return mapping.findForward("sitedist");
    }



    private long getTopic(HttpServletRequest request) {
        Object topic = request.getSession().getAttribute("topicId");
        if (topic == null) {
            return -1;
        }
        return (Long) topic;
    }
}
